package com.aier.school.service.impl;

import java.sql.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Component;

import com.aier.common.dao.ICommonDAO;
import com.aier.school.entity.SchoolHoliday;
import com.aier.school.entity.mapper.SchoolHolidayRowMapper;
import com.aier.school.service.ISchoolHolidaysService;

@Component("schoolHolidaysService")
public class SchoolHolidaysServiceImpl_J implements ISchoolHolidaysService{

    @Resource(name = "commonDao")
    private ICommonDAO commonDao;

    public void setCommonDao(ICommonDAO commonDao) {
        this.commonDao = commonDao;
    }
    
    @Override
    public Map<String, Object> searchscHolidaysByCondition(int page,
            int pagesize, int sid, String sdate, String edate) {
        String sql = " from school_holiday as sh where sid=? ";
        if (sdate!=null&&!sdate.equals("")){
            sql += " and sh.sdate >=DATE('" + sdate + "')";
        }
        if (edate!=null&&!edate.equals("")){
            sql += " and sh.edate <=DATE('" + edate + "')";
        }
        String sql_first = sql;
        sql += " order by sh.sdate desc";

        return commonDao
                .queryPage(
                        "select sh.* "
                                + sql, "select count(*) " + sql_first, page,
                        pagesize, new Object[] { sid }, new Object[] { sid },
                        new SchoolHolidayRowMapper(SchoolHolidayRowMapper.AND_DATESTR));    
    }

    
    @Override
    public int insertSchoolHoliday(Date sdate, Date edate,int sid,String describe) {
        SchoolHoliday sh = new SchoolHoliday();
        sh.setSid(sid);
        sh.setSdate(sdate);
        sh.setEdate(edate);
        sh.setDesinfo(describe);
        try {
            commonDao.insert(
                        SchoolHolidayRowMapper.getPreparedStatementCreator(sh),
                        new GeneratedKeyHolder());
        } catch (Exception e) {
            return 0;
        }
        return 1;
    }

    @Override
    public int deleteSchoolHoliday(Long hid) {
        if (commonDao.update("delete from school_holiday where hid=?",
                new Object[] { hid }) > 0) {
            return 1;
        } else {
            return 0;
        }
    }
    
    
}
